/**
 * 
 */
package com.boronstream.istar.http;

import java.util.HashMap;
import java.util.Map;

import android.util.Log;

import com.boronstream.istar.PlayApplication;
import com.boronstream.istar.http.PlayRequest.HttpMethodType;
import com.boronstream.istar.utils.CommonDefine;

/**
 * @author Stream
 * 
 */
public class DefaultPlayClient implements PlayClient {
	private static final String TAG = DefaultPlayClient.class.getSimpleName();
	private String requestUrl = "";

	@Override
	public <T extends PlayResponse> T execute(PlayRequest<T> request) {
		this.requestUrl = CommonDefine.APP_SERVER_URL
				+ request.getApiMethodName();
		if (request.getHttpMethodType() == HttpMethodType.GET) {
			return doGet(request);
		}
		if (request.getHttpMethodType() == HttpMethodType.POST) {
			return doPost(request);
		}
		if (request.getHttpMethodType() == HttpMethodType.POST_JSON) {
			return doPostJson(request);
		}
		return null;
	}

	private <T extends PlayResponse> T doGet(PlayRequest<T> request) {
		Map<String, Object> params = new HashMap<String, Object>();

		Map<String, Object> requestParams = request.getRequestParams();
		if (requestParams != null) {
			params.putAll(request.getRequestParams());
		}

		T response = null;
		try {
			response = request.getResponseClass().newInstance();
		} catch (Exception e) {

			Log.e(TAG, "doGet exception1 e == " + e.toString());
		}

		HashMap<String, String> headers = new HashMap<String, String>();
		headers.put("device_id", PlayApplication.device_id);
		// FIXME: fix the device id

		try {
			HashMap<String, String> string = WebUtils.doGet(this.requestUrl,
					headers, params);
			response.setBodyContent(string.get("body"));
			response.setStatusCode(Integer.valueOf(string.get("status_code")));
			response.parseResonseData();
		} catch (Exception e) {
			Log.e(TAG, "doGet exception2 e == " + e.toString());
			e.printStackTrace();
		}
		Log.d(TAG, "doGet before exit, response == " + response);
		return response;
	}

	private <T extends PlayResponse> T doPost(PlayRequest<T> request) {
		Map<String, Object> params = new HashMap<String, Object>();
		Map<String, Object> requestParams = request.getRequestParams();
		if (requestParams != null) {
			params.putAll(request.getRequestParams());
		}
//		params.put("device_id", PlayApplication.device_id);

		T response = null;
		try {
			response = request.getResponseClass().newInstance();
		} catch (Exception e) {

			Log.e(TAG,"doPost1:"+e.getMessage());
			e.printStackTrace();
		}

		HashMap<String, String> headers = new HashMap<String, String>();
//		headers.put("device_id", PlayApplication.device_id);
		// FIXME: fix the device id

		try {
			HashMap<String, String> string = WebUtils.doPost(this.requestUrl,
					headers, params);
			response.setBodyContent(string.get("body"));
			response.setStatusCode(Integer.valueOf(string.get("status_code")));
			response.parseResonseData();
		} catch (Exception e) {

			Log.e(TAG,"doPost2:"+e.getMessage());
			e.printStackTrace();
		}
		return response;
	}

	private <T extends PlayResponse> T doPostJson(PlayRequest<T> request) {


		T response = null;
		try {
			response = request.getResponseClass().newInstance();
		
		} catch (Exception e) {
			Log.e(TAG,"doPostJson1:"+e.getMessage());
		}

		HashMap<String, String> headers = new HashMap<String, String>();
		headers.put("Accept", "application/json");
		headers.put("Content-type", "application/json");
		headers.put("device_id", PlayApplication.device_id);
		// FIXME: fix the device id

		try {
			HashMap<String, String> string = WebUtils.doPostJson(
					this.requestUrl, headers, request.getJsonParams());
			response.setBodyContent(string.get("body"));
			response.setStatusCode(Integer.valueOf(string.get("status_code")));
			response.parseResonseData();
		} catch (Exception e) {
			Log.e(TAG,"doPostJson2:"+e.getMessage());
			e.printStackTrace();
		}
		return response;
	}
}
